<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <style>
        #app {
            background-color: #ccc;
            padding: 10px;
        }

        .child {
            background-color: skyblue;
            padding: 10px;
        }
    </style>
</head>

<body>
    <div id="app">
        <p>父组件</p>
        <!-- <p>msg:{{msg}}</p> -->
        <hr>
        <child-two ></child-two>
        <hr>
        <child-one ></child-one>
    </div>
    <template id="ChildOne">
        <div class="child">
            <p>子组件One</p>
            <p>msg:{{msg}}</p>
            <input type="button" value="获得MSG" @click="sentMsg()">
        </div>
    </template>
    <template id="ChildTwo">
        <div class="child">
            <p>子组件Two</p>
            <p>msg:{{msg}}</p>           
        </div>
    </template>
</body>
<script>
    var eventBus=new Vue({});
    eventBus.$on("aaa",function(){});
    // console.log(eventBus);
    Vue.component("ChildOne",{
        template:"#ChildOne",
        data(){
            return{
                msg:"子组件1的数据变量"
            }
        },
        methods:{
            // sentMsg(){
            //     eventBus.$emit("update:msg",this.msg);
            // }
            sentMsg(){
                this.$root.$emit("update:msg",this.msg);
            }
        },
        mounted(){
            // eventBus.$emit("update:msg",this.msg);
            console.log(this.$root);
            this.$root.$emit("update:msg",this.msg);
        }
    })
    Vue.component("ChildTwo",{
        template:"#ChildTwo",
        data(){
            return{
                msg:""
            }
        },
        mounted(){
            // eventBus.$on("update:msg",function(arg){
            //     console.log(this);
            //     this.msg=arg;
            // })
            eventBus.$on("update:msg",(arg)=>{
                // console.log(this);
                this.msg=arg;
            })
            // console.log(this.$root);
            this.$root.$on("update:msg",(arg)=>{
                this.msg=arg;
            })
        }
    })
    var vm=new Vue({
        el: "#app"
    });
    // vm.$on("update:msg",function(a){
    //     console.log("自定义事件=",a);
    // })
    // console.log(vm);
    // vm.$emit("update:msg","我不要你觉得，我只要我觉得!");
</script>

</html>